package com.aotain.project.apollo.app;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import com.aotain.common.Hurst;


/**
 * 数据分析 -Helios
 * @author Administrator
 *
 */
public class DataAlysHelios {
	
	public static void main(String[] args) {
		DataAlysHelios helios = new DataAlysHelios();
		//322.0|16.0|50.0|12.0|8.0|164.0|62.0|142.0|
		//144.0|6.0|22.0|10.0|24.0|27.0|24.0|
		//192.0|36.0|28.0|224.0|130.0|
		//68.0|2.0|8.0|20.0|4.0|18.0|6.0|
		ArrayList<Double> totalData = helios.ReadFile("D:\\temp\\hurst_test_data");
		//
		
//		ArrayList<Double> resultData = helios.Calculate(totalData);
//		
//		for(int i=0;i<resultData.size();i++)
//		{
//			System.out.println(String.format("%d,%f",i,resultData.get(i)));
//		}
		
		double[] value = new double[]{322.0,16.0,50.0,12.0,8.0,164.0,62.0,142.0};
		Hurst hurst = new Hurst(value);
		double v = hurst.Calculate();
		System.out.println("hurst=" + v);
	}
	
	public ArrayList<Double> ReadFile(String filePath)
	{
		try
	       {
			   ArrayList<Double> totalData = new ArrayList<Double>();
	    	   File f = new File(filePath);
	    	   if(f.isDirectory())
	    	   {
	    		   File files[] = f.listFiles();
	    		   //读取文件内容
	    		   for(File file : files)
	    		   {
	    			   FileReader fr = new FileReader(file);
	    			   BufferedReader br = new BufferedReader(fr);
	    	    	   //存放数据的临时变量
	    	    	   String lineData = null;
	    	    	   String[] splitData = null;
	    	      
	    	    	   boolean flag = false;
	    	    	   //按行读取
	    	    	   while(br.ready())
	    	    	   {//时间|IP|VALUE
	    	    		   lineData = br.readLine();
	    	    		   String[] items = lineData.split(",",-1);
	    	    		   double v = Double.parseDouble(items[2]);
	    	    		   totalData.add(v);
	    	    	   }
	    		   }
	    	   }
	    	   
	    	   return totalData;
	       }
		catch(Exception ex)
		{
			return null;
		}
	}
	
	public ArrayList<Double> Calculate(ArrayList<Double> totalData)
	{
		int nWindow = 30;
		int nTotal = totalData.size();
		int nStart = 0;
		
		ArrayList<Double> result = new ArrayList<Double>();
		
		while(nTotal - (nStart + nWindow )>=0)
		{
			double[] dWindows = new double[nWindow];
			int pos = 0;
			for(int i = nStart; i<nWindow+nStart; i++)
			{
				dWindows[pos] = totalData.get(i);
				pos++;
			}
			
			Hurst hurst = new Hurst(dWindows);
			result.add(hurst.Calculate());
			nStart++;
		}
		
		return result;
	}

}
